<template>
  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
    <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="110px" style="width: 80%; margin-left:50px;" :disabled="dialogStatus == 'look'">
      <el-row :gutter="40">
        <el-col :span="12">
          <el-form-item label="任务名称" prop="jobName">
            <el-input v-model="temp.jobName" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="任务状态" prop="checkStatus">
            <!-- <el-select v-model="temp.checkStatus" class="filter-item" placeholder="请选择任务状态" >
              <el-option
                v-for="item in dictList('is_use')"
                :key="'type' + item.label"
                :label="item.label"
                :value="item.value" />
            </el-select> -->
            <el-radio v-for="(item, index) in dictList('is_use')" :key="index" v-model="temp.checkStatus" :label="item.value" :disabled="dialogStatus=='update'">
              {{ item.label }}
            </el-radio> 
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="数据处理名称" prop="dataProcessingId">
            <!-- <el-input v-model="temp.dataProcessingId" /> -->
            <el-select v-model="temp.dataProcessName" class="filter-item" placeholder="请选择任务状态" @change="choseDataName" >
              <el-option
                v-for="item in list"
                :key="id"
                :label="item.dataProcessName"
                :value="{id:item.id,name:item.dataProcessName }" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="任务描述" prop="description">
            <el-input v-model="temp.description" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="cron表达式" prop="cronExpression">
            <el-input v-model="temp.cronExpression" @focus="dialogCron = true" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">
        {{ $t('table.cancel') }}
      </el-button>
      <el-button type="primary" :loading="loading" @click="dialogStatus === 'create' ? createData() :dialogStatus === 'look'?dialogFormVisible = false: updateData()">
        {{ $t('table.confirm') }}
      </el-button>
    </div>
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogCron" :append-to-body="true"
      :close-on-click-modal="false">
      <Crontab @fill="getStr" @hide="dialogCron = false"></Crontab>
    </el-dialog>
  </el-dialog>
</template>

<script>
import { createDataGovernanceTask, updateDataGovernanceTask, getDataGovernanceTask } from '@/api/datagovernancetask/dataGovernanceTask'
import { getmetadataJob } from '@/api/datagovernancetask/dataGovernanceTask'
import { deleteDataGovernance, fetchDataGovernanceList } from '@/api/datagovernance/dataGovernance'
import Crontab from "@/components/Crontab/index.vue"
export default {
  name: 'DataGovernanceTaskForm',
  components: { Crontab},
  data() {
    return {
      rules: {
        jobName: [{ required: true, message: '任务名称为必填项', trigger: 'blur' }],
                          checkStatus: [{ required: true, message: '任务状态为必填项', trigger: 'blur' }],
                          cronExpression: [{ required: true, message: 'cron表达式为必填项', trigger: 'blur' }],
                          dataProcessingId: [{ required: true, message: '数据处理id为必填项', trigger: 'blur' }]
                        },
      temp: {
        checkStatus:'1'
      },
      textMap: {
        update: '编辑',
        create: '新建',
        look:'查看'
      },
      loading: false,
      dialogFormVisible: false,
      dialogStatus: '',
      dialogCron: false,
      listQuery: {
        page: 1,
        limit: 999
      },
      list:[]
    }
  },
  created(){
    fetchDataGovernanceList(this.listQuery).then(response => {
      if(response.data.code==0){
        this.list = response.data.data
      }else{
        this.$message.error(response.data.msg)
      }
      })
  },
  methods: {
    getList() {
      this.$emit('refreshList')
    },
    choseDataName(data){
      console.log(data)
         this.temp.dataProcessName=data.name 
         this.temp.dataProcessingId=data.id
    },
    //获取返回的cron表达式
    getStr(str) {
      this.temp.cronExpression = str
    },
    setList(id){
      getmetadataJob(id).then(response => {
        if (response.data.code === 0) {
          this.temp = response.data.data
          this.list=[]
        } else {
          this.dialogFormVisible = false
          this.$message.error(response.data.msg)
        }

      })
     },
    resetTemp() {
      this.loading = false
      this.temp = {
        id: undefined,
              jobName: undefined,
              checkStatus: '1',
              checkDate: undefined,
              createBy: undefined,
              createDate: undefined,
              updateDate: undefined,
              updateBy: undefined,
              delFlag: undefined,
              tenantId: undefined,
              cronExpression: undefined,
              remarks: undefined,
              jobGroup: undefined,
              misfirePolicy: undefined,
              loadWay: undefined,
              description: undefined,
              isConcurrent: undefined,
              executeClass: undefined,
              methodName: undefined,
              methodParams: undefined,
              dataProcessingId: undefined
            }
    },
    handleLook(id){
      console.log(id)
      this.id = id
      this.resetTemp()
      this.dialogStatus = 'look'
      this.dialogFormVisible = true
      this.setList(id)
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.loading = true
          createDataGovernanceTask(this.temp).then(response => {
            this.loading = false
            if (response.data.code === 0) {
              this.getList()
              this.dialogFormVisible = false
              this.$message.success(response.data.msg)
            } else {
              this.$message.error(response.data.msg)
            }
          })
        }
      })
    },
    handleUpdate(id) {
      this.resetTemp()
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
      getDataGovernanceTask(id).then(response => {
        if (response.data.code === 0) {
          this.temp = response.data.data
        } else {
          this.dialogFormVisible = false
          this.$message.error(response.data.msg)
        }
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.loading = true
          updateDataGovernanceTask(this.temp).then(response => {
            this.loading = false
            if (response.data.code === 0) {
              this.dialogFormVisible = false
              this.getList()
              this.$message.success(response.data.msg)
            } else {
              this.$message.error(response.data.msg)
            }
          })
        }
      })
    }
  }
}
</script>